package net.cityside.jlearn.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.apache.commons.dbutils.DbUtils;

public class RepeatedCommitTest {

	public static void main(String[] args) {
		Connection conn = null;
		try {
			conn = DriverManager
					.getConnection("jdbc:mysql://localhost/yy?user=yy&password=yy");
			conn.setAutoCommit(false);

			PreparedStatement pst = conn
					.prepareStatement("insert into events(subject, details) values(?, ?)");

			pst.setString(1, "event1");
			pst.setString(2, "details1");
			System.out.println("insert 1:" + pst.executeUpdate());

			conn.commit();

			pst.setString(1, "event2");
			pst.setString(2, "details2");
			System.out.println("insert 2:" + pst.executeUpdate());

			pst.setString(1, "event3");
			pst.setString(2, "details3");
			System.out.println("insert 3:" + pst.executeUpdate());

			// conn.commit();
			conn.rollback();
			// Savepoint sp1 = conn.setSavepoint("sp1");

			pst.setString(1, "event4");
			pst.setString(2, "details4");
			System.out.println("insert 4:" + pst.executeUpdate());

			pst.setString(1, "event5");
			pst.setString(2, "details5");
			System.out.println("insert 5:" + pst.executeUpdate());

			// conn.rollback(sp1);
			conn.commit();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtils.closeQuietly(conn);
		}
	}
}
